package com.tjw.telecomver2.web;

import com.tjw.telecomver2.ex.CoolSharkServiceException;
import lombok.Data;

/**
 * 向前端返回的统一类型
 */
@Data
public class JsonResult<T> {

    public Integer state;

    public String message;

    public T data;

    public JsonResult() {
    }

    public static JsonResult<Void> OK(){
        return OK(null);
    }

    public static<T> JsonResult<T> OK(T data) {
        JsonResult<T> jsonResult = new JsonResult();
        jsonResult.setData(data);
        jsonResult.setState(ResponseCode.OK.getValue());
        jsonResult.setMessage("OK");
        return jsonResult;
    }

    public static JsonResult<Void> failed(CoolSharkServiceException e) {
        return failed(e.getResponseCode(), e.getMessage());
    }

    public static JsonResult<Void> failed(ResponseCode responseCode, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.setState(responseCode.getValue());
        jsonResult.setMessage(message);
        jsonResult.setData(null);
        return jsonResult;
    }
}
